﻿using ChurchDiary.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.Mvc;
using ChurchDiary.TableEntity;

namespace ChurchWebsite.Areas.Administration.Controllers
{
    public class ApplicationPagesController : Controller
    {
        //
        // GET: /Administration/ApplicationPages/
        #region Pages
        public ActionResult Pages()
        {
            return View();
        }
        public JsonResult GetPages(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Administration().GetPages(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetPagesAll()
        {
            return Json(new BLL_Administration().GetPages(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateNewPages()
        {
            string IResult = "";
            int iCount = 0;
            int uCount = 0;
            EntityPage Page = new EntityPage();
            var controllers = Assembly.GetExecutingAssembly().GetExportedTypes().Where(t => typeof(ControllerBase).IsAssignableFrom(t)).Select(t => t);
            foreach (Type controller in controllers)
            {
                Page.ControllerName = controller.Name;
                var actions = controller.GetMethods().Where(t => t.Name != "Dispose" && !t.IsSpecialName && t.DeclaringType.IsSubclassOf(typeof(ControllerBase)) && t.IsPublic && !t.IsStatic).ToList();
                foreach (var action in actions)
                {
                    Page.ActionName = action.Name;
                    if(action.ReturnType.Name=="ActionResult")
                    {
                        IResult = new BLL_Administration().InsertUpdateNewPages(Page);
                        if (IResult == "ISUS")
                        {
                            iCount += 1;
                        }
                        else if (IResult == "USUS")
                        {
                            uCount += 1;
                        }
                    }
                }
            }
            return Json(iCount.ToString() + " inserted & " + uCount.ToString() + " Updated" , JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetPage(int dataId)
        {
            return Json(new BLL_Administration().GetPage(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdatePage(EntityPage Pages)
        {
            return Json(new BLL_Administration().UpdatePage(Pages), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region Page SectionPk
        public ActionResult PageSections()
        {
            return View();
        }
        public JsonResult GetPageSectionPks(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Administration().GetPageSectionPk(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetPageSectionPk(Guid dataId)
        {
            return Json(new BLL_Administration().GetPageSectionPk(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetPageSectionPkAll()
        {
            return Json(new BLL_Administration().GetPageSectionPk(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddPageSectionPk(EntityPageSectionPk PageSection) 
        {
            return Json(new BLL_Administration().InsertPageSectionPk(PageSection), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdatePageSectionPk(EntityPageSectionPk PageSection)
        {
            return Json(new BLL_Administration().UpdatePageSectionPk(PageSection), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeletePageSectionPk(Guid dataId)
        {
            return Json(new BLL_Administration().DeletePageSectionPk(dataId), JsonRequestBehavior.AllowGet);
        }
        #endregion Page Section
        #region PageSection
        public ActionResult PageSectionInfo(Guid SectionId)
        {
            return View();
        }
        public ActionResult PageSectionEdit(Guid PageSectionId)
        {
            ViewBag.PageHtml = new BLL_Administration().GetPageSection(PageSectionId);
            return View();
        }
        public JsonResult GetPageSections(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage, Guid PageSectionId)
        {
            return Json(new BLL_Administration().GetPageSection(PageNo, SearchKey, Sort, SortMode, PerPage, PageSectionId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetPageSection(Guid dataId)
        {
            return Json(new BLL_Administration().GetPageSection(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddPageSection(EntityPageSection PageSections)
        {
            return Json(new BLL_Administration().InsertPageSection(PageSections), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdatePageSection(EntityPageSection PageSections)
        {
            return Json(new BLL_Administration().UpdatePageSection(PageSections), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeletePageSection(Guid dataId)
        {
            return Json(new BLL_Administration().DeletePageSection(dataId), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region PageSection Assignment
        public ActionResult PageSectionAssignment()
        {
            return View();
        }
        public JsonResult GetPageSectionAssignments(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Administration().GetPageSectionAssignment(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetPageSectionAssignment(Guid dataId)
        {
            return Json(new BLL_Administration().GetPageSectionAssignment(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddPageSectionAssignment(EntityPageSectionAssignment Assignment)
        {
            return Json(new BLL_Administration().InsertPageSectionAssignment(Assignment), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdatePageSectionAssignment(EntityPageSectionAssignment Assignment)
        {
            return Json(new BLL_Administration().UpdatePageSectionAssignment(Assignment), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeletePageSectionAssignment(Guid dataId)
        {
            return Json(new BLL_Administration().DeletePageSectionAssignment(dataId), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
